#include "mydb.h"

MyDB::MyDB(QObject *parent)
    : QObject{parent}
{
    /*打开数据库*/
    m_db = QSqlDatabase::addDatabase("QSQLITE");
    m_db.setDatabaseName("mydb.db");
    m_db.open();
}

MyDB *MyDB::getInstance()
{
    static MyDB* myDB = new MyDB();
    return myDB;
}

void MyDB::addStudent(QString id, QString name, QString gender, QString age)
{
    QString sql = QString("insert into student values (\"%1\",\"%2\",\"%3\",\"%4\")")
    .arg(id).arg(name).arg(gender).arg(age);
    QSqlQuery query(sql);
}

void MyDB::delStudent(QString id)
{
    QString sql = QString("delete from student where id = \"%1\"")
    .arg(id);
    QSqlQuery query(sql);
}

void MyDB::addCourse(QString id, QString name, QString credit)
{
    QString sql = QString("insert into course values (\"%1\",\"%2\",\"%3\")")
    .arg(id).arg(name).arg(credit);
    QSqlQuery query(sql);
}

void MyDB::delCourse(QString id)
{
    QString sql = QString("delete from course where id = \"%1\"")
    .arg(id);
    qDebug() << sql;
    QSqlQuery query(sql);
}

void MyDB::addGrade(QString sid, QString cid, QString grade)
{
    QString sql = QString("insert into grade values (\"%1\",\"%2\",\"%3\")")
    .arg(sid).arg(cid).arg(grade);
    QSqlQuery query(sql);
}

void MyDB::delGrade(QString sid, QString cid)
{
    QString sql = QString("delete from grade where sid = \"%1\" and cid = \"%2\"")
    .arg(sid).arg(cid);
    qDebug() << sql;
    QSqlQuery query(sql);
}
